package com.fhz.day003.utils;

import com.fhz.day003.R;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 时间:2022/3/22
 *
 * @author msfeng
 * 简述: TODO
 */
public class RetrofitManager {

    private static RetrofitManager retrofitManager;

    private Retrofit retrofit = null;
    private RetrofitManager(){

    }

    public static RetrofitManager getInstance(){
        if(retrofitManager == null){//1
            synchronized (RetrofitManager.class){ //类锁,和对象锁
                if(retrofitManager == null){
                    return retrofitManager =  new RetrofitManager();
                }
            }
        }
        return retrofitManager;
    }

    public synchronized <T> T create(Class<T> tClass){
        if(retrofit == null){
            retrofit = new Retrofit.Builder()
                    .baseUrl("http://www.fenghongzhang.com:9999/")
                    .client(new OkHttpClient().newBuilder()
                            .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(new LiveDataCallAdapterFactory())
                    .build();
            return retrofit.create(tClass);
        }
        return retrofit.create(tClass);
    }
}

